﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="student_detail.aspx.cs" Inherits="SQT.tracnghiem.Ajax.ASP.Class.student_detail" %>

<%@ Import Namespace="DataAccess" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<body>
    <%
        int id = int.Parse(Request.QueryString["id"]);
        var get_student = new TblUserController().FetchAll().Where(x => x.UserId == id).Single();
        string cls = "";
        string sex = "Nam";
        string type = "";
        string open = "";
        string close = "";
        string time = "";
        string degree = "";
        string date_from = "";
        string to = "";
        string level = "";
        string country = "";
        string address = "";
        string pay_date = "";
        string exp_date = "";
        string number_study = "";
        if (get_student.ClassX != null)
        {
            var get_cls = new TblClassController().FetchAll().Where(x => x.Id == get_student.ClassX).Single();
            cls = get_cls.ClassName;
            type = get_cls.ClsLevel;
            if (get_cls.OpenDate != null)
            {
                open = DateTime.Parse(get_cls.OpenDate.ToString()).Day + "/" + DateTime.Parse(get_cls.OpenDate.ToString()).Month + "/" + DateTime.Parse(get_cls.OpenDate.ToString()).Year;
                date_from = DateTime.Parse(get_cls.OpenDate.ToString()).Year.ToString();
                date_from += "-";
                if (DateTime.Parse(get_cls.OpenDate.ToString()).Month < 10)
                    date_from += "0" + DateTime.Parse(get_cls.OpenDate.ToString()).Month.ToString();
                else
                    date_from += DateTime.Parse(get_cls.OpenDate.ToString()).Month.ToString();
                date_from += "-";
                if (DateTime.Parse(get_cls.OpenDate.ToString()).Day < 10)
                    date_from += "0" + DateTime.Parse(get_cls.OpenDate.ToString()).Day.ToString();
                else
                    date_from += DateTime.Parse(get_cls.OpenDate.ToString()).Day.ToString();
            }
            if (get_cls.CloseDate != null)
                close = DateTime.Parse(get_cls.CloseDate.ToString()).Day + "/" + DateTime.Parse(get_cls.CloseDate.ToString()).Month + "/" + DateTime.Parse(get_cls.CloseDate.ToString()).Year;

            if (get_cls.TotalTime != null)
                time = get_cls.TotalTime;

            if (get_cls.Degree != null)
                degree = get_cls.Degree;

            if (get_cls.ClsLevel != null)
                level = get_cls.ClsLevel;
        }
        if (get_student.Sex == false)
            sex = "Nữ";

        to = DateTime.Now.Year.ToString();
        to += "-";
        if (DateTime.Now.Month < 10)
            to += "0" + DateTime.Now.Month.ToString();
        else
            to += DateTime.Now.Month.ToString();
        to += "-";
        if (DateTime.Now.Day < 10)
            to += "0" + DateTime.Now.Day.ToString();
        else
            to += DateTime.Now.Day.ToString();
        if (get_student.Address != null)
        {
            var get_city = new TblCityController().FetchAll().Where(x => x.Id == get_student.Address).Single();
            var get_province = new TblProvinceController().FetchAll().Where(x => x.Id == get_city.ProvinceId).Single();
            country = get_city.CityName + " - " + get_province.ProvinceName;
        }
        if (get_student.AddressMail != null)
            address = get_student.AddressMail;
        var get_bill = new TblStudyFeeController().FetchAll().Where(x => x.UserId == get_student.UserId && x.Active == true).ToList();
        if (get_bill.Count != 0)
        {
            var get_last_bill = new TblStudyFeeController().FetchAll().Where(x => x.UserId == get_student.UserId && x.Active == true).Last();
            pay_date = DateTime.Parse(get_last_bill.PayDate.ToString()).Day.ToString() + "/" + DateTime.Parse(get_last_bill.PayDate.ToString()).Month.ToString() + "/" + DateTime.Parse(get_last_bill.PayDate.ToString()).Year.ToString();
            exp_date = DateTime.Parse(get_last_bill.ExpDate.ToString()).Day.ToString() + "/" + DateTime.Parse(get_last_bill.ExpDate.ToString()).Month.ToString() + "/" + DateTime.Parse(get_last_bill.ExpDate.ToString()).Year.ToString();
        }
        if (get_student.NumberStudy != "")
            number_study = get_student.NumberStudy;
    %>
    <header class="class-content-header w3-display-container w3-teal">
        Đánh giá chuyên cần và Thành tích học tập
        <i onclick="close_student_detail()" class="close-student-detail-btn fa fa-times-circle w3-closebtn w3-text-white w3-display-topright" title="Quay lại thống kê"></i>
    </header>
    <div class="w3-padding w3-center">
        <label>Từ ngày</label>&nbsp;<input id="from" value="<%=date_from %>" class="class-editor" type="date" />&nbsp;<label>Đến ngày</label>&nbsp;<input id="to" value="<%=to %>" class="class-editor" type="date" />&nbsp;<a class="btn_detail_date w3-btn w3-teal"><i class="fa fa-list"></i></a>
    </div>
    <div class="class-student-detail">
        <ul class="w3-navbar w3-border-bottom w3-border-teal">
            <li class="w3-white"><a href="javascript:;" class="tablink w3-teal" onclick="openResult(event, 'thongtin');">Thông tin</a></li>
            <li class="w3-white"><a href="javascript:;" class="tablink" onclick="openResult(event, 'chuyencan');">Chuyên cần</a></li>
            <li class="w3-white"><a href="javascript:;" class="tablink" onclick="openResult(event, 'kiemtra');">Kiểm tra</a></li>
            <li class="w3-white"><a href="javascript:;" class="tablink" onclick="openResult(event, 'kynang');">Kỹ năng cá nhân</a></li>
        </ul>
        <div id="thongtin" class="class-student-results w3-padding w3-border w3-border-teal w3-row w3-white">
            <div class="w3-col l2 w3-padding-top">
                <img class="student-info-img w3-card-2" src="../Content/avatar/<%=get_student.Avatar %>" alt="student photo" />
            </div>
            <div class="w3-col l5">
                <p style="display: none">
                    <label id="student_id"><%=get_student.UserId %></label>
                </p>
                <p>
                    <label>Mã học viên:&nbsp;</label><label><%=number_study %></label>
                </p>
                <p>
                    <label>Họ tên học viên:&nbsp;</label><%=get_student.FullName %>
                </p>
                <p>
                    <label>Ngày sinh:&nbsp;</label><%=DateTime.Parse(get_student.BirthDay.ToString()).Day %>/<%=DateTime.Parse(get_student.BirthDay.ToString()).Month %>/<%=DateTime.Parse(get_student.BirthDay.ToString()).Year %>
                </p>
                <p>
                    <label>Giới tính:&nbsp;</label><%=sex %>
                </p>
                <p>
                    <label>Email:&nbsp;</label><%=get_student.Email %>
                </p>
                <p>
                    <label>Số điện thoại:&nbsp;</label><%=get_student.Phone %>
                </p>
                <p>
                    <label>Quê quán:&nbsp;</label><%=country %>
                </p>
                <p>
                    <label>Địa chỉ:&nbsp;</label><%=address %>
                </p>
            </div>
            <div class="w3-col l5">
                <p>
                    <label>Lớp:&nbsp;</label><%=cls %>
                </p>
                <p>
                    <label>Hình thức học:&nbsp;</label><%=degree %>
                </p>
                <p>
                    <label>Ngày nhập học:&nbsp;</label><%=open %>
                </p>
                <p>
                    <label>Ngày tốt nghiệp:&nbsp;</label><%=close %>
                </p>
                <p>
                    <label>Trình độ:&nbsp;</label><%=level %>
                </p>
                <%--<p>
                    <label>Học phí:&nbsp;</label>
                </p>
                <p>
                    <label>Gói:&nbsp;</label>
                </p>--%>
                <p>
                    <label>Ngày đóng:&nbsp;</label>
                    <%=pay_date %>
                </p>
                <p>
                    <label>Thời hạn học phí:&nbsp;</label>
                    <%=exp_date %>
                </p>
            </div>
        </div>
        <%
            var get_check_total = new TblCheckUserController().FetchAll().Where(x => x.UserId == id && DateTime.Parse(x.DateX.ToString()).Date >= DateTime.Parse(date_from).Date && DateTime.Parse(x.DateX.ToString()).Date <= DateTime.Now.Date);
            int count = 0, early = 0, late = 0, absent_yes = 0, absent_no = 0;
            foreach (TblCheckUser chk in get_check_total)
            {
                if (chk.Early == true)
                    early++;
                else if (chk.Late == true)
                    late++;
                else if (chk.AbsentYes == true)
                    absent_yes++;
                else if (chk.AbsentNo == true)
                    absent_no++;
                count++;
            }
            int rate = 0;
            if (count != 0)
                rate = early * 100 / count;
        %>
        <div id="chuyencan" class="class-student-results w3-responsive w3-border w3-border-teal" style="display: none">
            <table class="w3-table w3-bordered">
                <tr>
                    <th class="w3-teal">Tổng số ngày học tiêu chuẩn</th>
                    <td><%=count %></td>
                </tr>
                <tr>
                    <th class="w3-teal">Số ngày đi học đúng giờ</th>
                    <td><%=early %></td>
                </tr>
                <tr>
                    <th class="w3-teal">Số ngày đi học trễ hoặc về sớm</th>
                    <td><%=late %></td>
                </tr>
                <tr>
                    <th class="w3-teal">Số ngày vắng có phép</th>
                    <td><%=absent_yes %></td>
                </tr>
                <tr>
                    <th class="w3-teal">Số ngày vắng không phép</th>
                    <td><%=absent_no %></td>
                </tr>
                <tr>
                    <th class="w3-teal">Tỷ lệ đi học đúng giờ (%)</th>
                    <td><%=rate %>%</td>
                </tr>
            </table>
        </div>
        <% 
            var get_result = (from p in new TblResultController().FetchAll() orderby p.ResultId descending where p.ResultUser == id && DateTime.Parse(p.ResultDate.ToString()).Date >= DateTime.Parse(date_from).Date && DateTime.Parse(p.ResultDate.ToString()).Date <= DateTime.Now.Date select p);
        %>
        <div id="kiemtra" class="class-student-results w3-responsive w3-border w3-border-teal" style="display: none">
            <table class="w3-table w3-striped w3-bordered">
                <tr class="w3-teal">
                    <th>Tên bài</th>
                    <th>Ngày làm</th>
                    <th>Hoàn thành</th>
                    <th>Từ vựng</th>
                    <th>Ngữ pháp</th>
                    <th>Nghe</th>
                    <th>Hán tự</th>
                    <th>Tổng điểm</th>
                    <th>&nbsp;</th>
                </tr>
                <%foreach (TblResult result in get_result)
                    {
                        var get_test = new TblTestController().FetchAll().Where(x => x.TestId == result.TestId).Single();

                        if (get_test.TestExam != true)
                            continue;
                        string result_user = result.ResultAnswer;
                        int tuvung = 0, nguphap = 0, nghe = 0, hantu = 0;

                        var get_tuvung = new TblTestDetailController().FetchAll().Where(x => x.TestId == get_test.TestId && x.TestDetailType == 1).ToList();
                        var get_nguphap = new TblTestDetailController().FetchAll().Where(x => x.TestId == get_test.TestId && x.TestDetailType == 2).ToList();
                        var get_nghe = new TblTestDetailController().FetchAll().Where(x => x.TestId == get_test.TestId && x.TestDetailType == 3).ToList();
                        var get_hantu = new TblTestDetailController().FetchAll().Where(x => x.TestId == get_test.TestId && x.TestDetailType == 4).ToList();
                        int td_count = new TblTestDetailController().FetchAll().Where(x => x.TestId == get_test.TestId).ToList().Count;
                        float score = 100f / td_count;
                        string[] split = new string[result_user.Split(',').Length];
                        string[] split2 = new string[2];
                        split = result_user.Split(',');
                        for (int i = 0; i < result_user.Split(',').Length - 1; i++)
                        {
                            split2 = split[i].Split('-');
                            int ok = 0;
                            var get_td = new TblTestDetailController().FetchAll().Where(x => x.TestDetailId == int.Parse(split2[0])).Single();
                            if (get_td.TestDetailConfirm == int.Parse(split2[1]))
                                ok = 1;
                            if (ok == 1)
                            {
                                if (get_td.TestDetailType == 1)
                                    tuvung++;
                                else if (get_td.TestDetailType == 2)
                                    nguphap++;
                                else if (get_td.TestDetailType == 3)
                                    nghe++;
                                else if (get_td.TestDetailType == 4)
                                    hantu++;
                            }
                        }
                        score = score * (tuvung + nguphap + nghe + hantu);
                %>
                <tr>
                    <td><%=get_test.TestTitle%></td>
                    <td><%=DateTime.Parse(result.ResultDate.ToString()).Day %>/<%=DateTime.Parse(result.ResultDate.ToString()).Month %>/<%=DateTime.Parse(result.ResultDate.ToString()).Year %></td>
                    <td><%=result.ResultTime %>/<%=get_test.TestTime %> phút</td>
                    <td><%=tuvung %>/<%=get_tuvung.Count %></td>
                    <td><%=nguphap %>/<%=get_nguphap.Count %></td>
                    <td><%=nghe %>/<%=get_nghe.Count %></td>
                    <td><%=hantu %>/<%=get_hantu.Count %></td>
                    <td><%=float.Parse(String.Format("{0:00.0}", score)) %></td>
                    <td class="result-detail-btn"><a href="../trac-nghiem/ket-qua-<%=result.ResultId %>" target="_blank" class="w3-text-teal">Chi tiết&nbsp;<i class="fa fa-angle-right"></i></a></td>
                </tr>
                <%} %>
            </table>
        </div>

        <%
            int word = 0, grammar = 0, listen = 0, write = 0, talk = 0, word_count = 0, grammar_count = 0, listen_count = 0, write_count = 0, talk_count = 0, word_total=0, grammar_total=0, listen_total=0, write_total=0, talk_total=0;
            var get_score= new TblExamScoreController().FetchAll().Where(x => x.ScoreStudentId == get_student.UserId);
            foreach (TblExamScore score in get_score)
            {
                if (score.ScoreWrite != null)
                {
                    word += (int)score.ScoreWrite;
                    word_count++;
                }
                if (score.ScoreGrammar != null)
                {
                    grammar += (int)score.ScoreGrammar;
                    grammar_count++;
                }
                if (score.ScoreListen != null)
                {
                    listen += (int)score.ScoreListen;
                    listen_count++;
                }
                if (score.ScoreKanji != null)
                {
                    write += (int)score.ScoreKanji;
                    write_count++;
                }
                if (score.ScoreSpeak != null)
                {
                    talk += (int)score.ScoreSpeak;
                    talk_count++;
                }
            }
            if(word_count!=0)
            {
                word_total = word / word_count;
            }
            if(grammar_count!=0)
            {
                grammar_total = grammar / grammar_count;
            }
            if(listen!=0)
            {
                listen_total = listen / listen_count;
            }
            if(write_count!=0)
            {
                write_total = write / write_count;
            }
            if(talk_count!=0)
            {
                talk_total = talk / talk_count;
            }
        %>
        <div id="kynang" class="class-student-results w3-responsive w3-border w3-border-teal w3-white" style="display: none">
            <canvas id="student_skill"></canvas>
            <div id="skill_text" style="display:none"> Kỹ năng cá nhân của học viên <%=get_student.FullName %></div>
            <div id="skill_label" style="display:none">
                <%--<div class="skill_label_child">Max</div>
                <div class="skill_label_child">Min</div>--%>
                <div class="skill_label_child">Từ vựng</div>
                <div class="skill_label_child">Ngữ pháp</div>
                <div class="skill_label_child">Nghe</div>
                <div class="skill_label_child">Hán tự</div>
                <div class="skill_label_child">Giao tiếp</div>
            </div>
            <div id="skill_data" style="display:none">
                <%--<div class="skill_data_child">100</div>
                <div class="skill_data_child">0</div>--%>
                <div class="skill_data_child"><%=word_total %></div>
                <div class="skill_data_child"><%=grammar_total %></div>
                <div class="skill_data_child"><%=listen_total %></div>
                <div class="skill_data_child"><%=write_total %></div>
                <div class="skill_data_child"><%=talk_total %></div>
            </div>
        </div>
    </div>

    <div class="w3-padding w3-center">
        <a class="w3-btn w3-teal" title="Gửi kết quả"><i class="fa fa-envelope"></i>&nbsp;Gửi kết quả</a>
        <a onclick="document.getElementById('chuyenlop').style.display='block'" class="w3-btn w3-teal" title="Chuyển lớp"><i class="fa fa-reply"></i>&nbsp;Chuyển lớp</a>
        <a onclick="document.getElementById('duoihoc').style.display='block';document.getElementById('remove_student_id').innerHTML='<%=id %>'" class="w3-btn w3-teal" title="Buộc thôi học"><i class="fa fa-rocket"></i>&nbsp;Buộc thôi học</a>
    </div>
</body>
</html>
